<?php
/**
 * 采购订单合同文件模型
 * (model)
 */
namespace App\Models\Purchase;

use Illuminate\Database\Eloquent\Model;
class PurchaseOrderContract extends Model
{
    protected $table = 'purchase_order_contract';

    protected $guarded = [];

    //文件类型
    public static $_type = [
        1 => 'PDF',
        2 => 'EXCEL',
    ];

    public static $_partyA = [
        1 => '美霖达科技有限公司',
        2 => '广东新钱潮信息科技有限公司',
        3 => '深圳思维科技有限责任公司',
    ];

    //文件
    const FILE_NAME_DIR = 'contract';
    const FILE_DIR = 'app'.DIRECTORY_SEPARATOR.self::FILE_NAME_DIR;
    public static function getFile($purchaseNo,$type){
        $fileDir = self::fileDir();
        $fileName = self::fileName($purchaseNo,$type);
        $filePath = storage_path($fileDir).DIRECTORY_SEPARATOR.$fileName;
        $fileDir = dirname($filePath);
        if(!is_dir($fileDir)){
            mkdir($fileDir,0777,true);
            chmod($fileDir, 0777);
        }
        return $filePath;
    }

    public static function fileDir(){
        return self::FILE_DIR;
    }

    public static function fileName($purchaseNo,$type){
        return $purchaseNo.'-PurchaseContractFile.'.($type == 1 ? 'pdf' : 'xlsx');
    }

    public static function storeFile($purchaseNo,$type){
        if(self::isExist($purchaseNo,$type) == 0){
            return self::query()->create([
                'purchase_no' => $purchaseNo,
                'type' => $type,
            ]);
        }
        return true;
    }

    public static function isExist($purchaseNo,$type){
        return self::query()
            ->where('purchase_no',$purchaseNo)
            ->where('type',$type)
            ->count();
    }
}
